using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Blog.IService;
using Blog.Command;
using AutoMapper;
using SqlSugar;
using Blog.Model.DTO;
using Blog.Model;
using Microsoft.AspNetCore.Authorization;

namespace Blog.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class BlogNewControllers :ControllerBase
    {
        private readonly IBlogNewsService _iBlogNewsService;

        public BlogNewControllers(IBlogNewsService iBlogNewsService)
        {
            _iBlogNewsService = iBlogNewsService;
        }

        [HttpPost]
        public async Task<ActionResult<ApiResult>> CreateAsync(string title, string context, int typeId)
        {
            BlogNews blognews = new()
            {
                BrovseCount = 0,
                Content = context,
                Title = title,
                Time = DateTime.Now,
                LikeCount = 0,
                TypeId = typeId,
                WriterId = Convert.ToInt32(this.User.FindFirst("Id").Value)
            };
            bool bl = await _iBlogNewsService.CreateAsync(blognews);
            if (!bl)
                return ApiResultHelper.Error("文章保存失败");
            return ApiResultHelper.Success("文章保存成功");
        }

        [HttpGet]
        public async Task<ActionResult<ApiResult>> QueryBlogNews()
        {   int id = Convert.ToInt32(this.User.FindFirst("Id").Value);
            var data = await _iBlogNewsService.QueryAsync(c => c.WriterId == id);
            if (data.Count == 0)
                return ApiResultHelper.Error("没有更多的文章");
            return ApiResultHelper.Success(data);
        }

        [HttpDelete]
        public async Task<ActionResult<ApiResult>> DeleteAsync(int id)
        {
            bool bl = await _iBlogNewsService.DeleteAsync(id);
            if (!bl)
                return ApiResultHelper.Error("删除文章失败");
            return ApiResultHelper.Success("删除文章成功");
        }

        [HttpPut]
        public async Task<ActionResult<ApiResult>> EditAsync(int id, string title, string context, int typeId)
        {
            var blogNews = await _iBlogNewsService.FindAsync(id);
            if (blogNews == null)
                return ApiResultHelper.Error("没有找到该文章");
            blogNews = new()
            {
                Title = title,
                Content = context,
                TypeId = typeId
            };
            bool bl = await _iBlogNewsService.EditAsync(blogNews);
            if (!bl)
                return ApiResultHelper.Error("文章修改失败");
            return ApiResultHelper.Success("文章更新成功");
        }

        [HttpGet]
        public async Task<ActionResult<ApiResult>> QueryPage([FromServices] IMapper iMapper, int page, int size)
        {
            RefAsync<int> total = 0;
            var blogNews = await _iBlogNewsService.QueryAsync(page, size, total);
            try
            {
                var blogDTO = iMapper.Map<List<BlogNewDTO>>(blogNews);
                return ApiResultHelper.Success(blogDTO, total);
            }
            catch (Exception ex)
            {
                return ApiResultHelper.Error("AutoMapper映射错误： " + ex);
            }
        }
    }
}